$('.uppic').error(function(){
    $(this).attr('src','http://tcsc.oss-cn-hangzhou.aliyuncs.com/static/all/uppic.png')
});
$('.del_img').click(function(){
    $(this).siblings('dt').find('input[type="hidden"]').val('')
    $(this).siblings('dt').find('img').attr('src','http://tcsc.oss-cn-hangzhou.aliyuncs.com/static/all/uppic.png')
});
function photoCompress(file,w,objDiv){
    var ready=new FileReader();
    /*开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容.*/
    ready.readAsDataURL(file);
    ready.onload=function(){
        var re=this.result;
        canvasDataURL(re,w,objDiv,file.type)
    }
}
function canvasDataURL(path, obj, callback,type){
    var img = new Image();
    img.src = path;
    img.onload = function(){
        var that = this;
        // 默认按比例压缩
        var w = that.width,
            h = that.height,
            scale = w / h;
            w = obj.size || w;  
            h = (obj.size/scale) || (w / scale);
            if(w>=5000){
                w=5000
                h=(w / scale)
            }
        var quality = 1;  // 默认图片质量为0.7
        //生成canvas
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        // 创建属性节点
        var anw = document.createAttribute("width");
        var anh = document.createAttribute("height");
        if(obj.avatar==1){
            if (w>h){
                anw.nodeValue = h;
                anh.nodeValue = h;
            }else{
                anw.nodeValue = w;
                anh.nodeValue = w;
            }
        }else{
            anw.nodeValue = w;
            anh.nodeValue = h;
        }
        canvas.setAttributeNode(anw);
        canvas.setAttributeNode(anh);
        ctx.drawImage(that, 0, 0, w, h);
        // 图像质量
        if(obj.quality && obj.quality <= 1 && obj.quality > 0){
            quality = obj.quality;
        }

        // quality值越小，所绘制出的图像越模糊
        var base64 = canvas.toDataURL(type, quality);
        // 回调函数返回base64的值
        callback(base64)
    }
}
function convertBase64UrlToBlob(urlData){
    var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type:mime});
}

function uppic(e, where, type,size,isavatar) {
    $('body').toast({
        position:'fixed',
        animateIn:'fadeIn',
        animateOut:'fadeOut',
        content:'上传中...',
        duration:'99999999',
        isCenter:true,
    });
    var fileObj = e.files[0]
    var form = new FormData()
     if(fileObj.type=="image/jpeg"||fileObj.type=="image/jpg"||fileObj.type=="image/png"){
        var final;
        switch(fileObj.type){
            case "image/jpeg":
            final=".jpg"
            break
            case "image/png":
            final=".png"
            break 
            case "image/jpg":
            final=".jpg"
            break
        }
        form.append("where",where);
        var that=$(e)
        var t=type||0; //0返回id,1返回路径
        var s=size
        var avatar=0||isavatar
        // if(fileObj.size/1024 > 100){
        //     photoCompress(fileObj, {
        //         quality: 0.5,size:s,avatar:avatar
        //     }, function(base64Codes){
        //         //console.log("压缩后：" + base.length / 1024 + " " + base);
        //        var bl = convertBase64UrlToBlob(base64Codes);
        //        form.append("file", bl, "file_"+Date.parse(new Date())+final); // 文件对象
        //        $.ajax({
        //             url:"/tcsc/sword_uppic",
        //             data:form,
        //             type: "post",
        //             dataType: "json",
        //             async: false,
        //             cache: false,//上传文件无需缓存
        //             processData: false,//用于对data参数进行序列化处理 这里必须false
        //             contentType: false, //必须
        //             success :function(res){
        //                 if (res.status==0) {
        //                      window.wxc.xcConfirm("图片上传失败,请重新上传","error")
        //                 }else{
        //                     var r=$('body').toast({
        //                         position:'fixed',
        //                         animateIn:'fadeIn',
        //                         animateOut:'fadeOut',
        //                         content:'上传成功',
        //                         duration:'500',
        //                         isCenter:true,
        //                     });
        //                     that.siblings('img').attr('src',res.pathname);
        //                     if(t==0){
        //                         that.siblings('input[type="hidden"]').val(res.mid);
        //                     }else{
        //                         that.siblings('input[type="hidden"]').val(res.pathname);
        //                     }
        //                 }
        //             },error:function(res){
        //                 mui.toast("网络繁忙,请重试~")
        //                 loadclose()
        //             }
        //         })
        //     });
        // }else{
            photoCompress(fileObj, {
                size:s,avatar:avatar
            }, function(base64Codes){
                //console.log("压缩后：" + base.length / 1024 + " " + base);
                var bl = convertBase64UrlToBlob(base64Codes);
                form.append("file", bl, "file_"+Date.parse(new Date())+final); // 文件对象
                $.ajax({
                    url:"/tcsc/sword_uppic",
                    data:form,
                    type: "post",
                    dataType: "json",
                    async: false,
                    cache: false,//上传文件无需缓存
                    processData: false,//用于对data参数进行序列化处理 这里必须false
                    contentType: false, //必须
                    success :function(res){
                        if (res.status==0) {
                             window.wxc.xcConfirm("图片上传失败,请重新上传","error")
                        }else{
                            var r=$('body').toast({
                                position:'fixed',
                                animateIn:'fadeIn',
                                animateOut:'fadeOut',
                                content:'上传成功',
                                duration:'500',
                                isCenter:true,
                            });
                            that.siblings('img').attr('src',res.pathname);
                            if(t==0){
                                that.siblings('input[type="hidden"]').val(res.mid);
                            }else{
                                that.siblings('input[type="hidden"]').val(res.pathname);
                            }
                        }
                    },error:function(res){
                        var r=$('body').toast({
                            position:'fixed',
                            animateIn:'fadeIn',
                            animateOut:'fadeOut',
                            content:'网络繁忙,请重试',
                            duration:'500',
                            isCenter:true,
                        });
                    }
                })
            });
        // };
        
    }else{
        window.wxc.xcConfirm("文件格式不正确","error")
    }
};

function fileup(e,where,type){
    var that=$(e)
    console.log(that)
    $('body').toast({
        position:'fixed',
        animateIn:'fadeIn',
        animateOut:'fadeOut',
        content:'上传中...',
        duration:'99999999',
        isCenter:true,
    });
    var t=type||0;
    var file=e.files[0];
    var form = new FormData();
    form.append("where",where)
    form.append("file",file)
    $.ajax({
        url:"/file",
        data:form,
        type: "post",
        dataType: "json",
        cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须
        success:function(res){
            if (res.status==0) {
                 window.wxc.xcConfirm("图片上传失败,请重新上传","error")
            }else{
                var r=$('body').toast({
                    position:'fixed',
                    animateIn:'fadeIn',
                    animateOut:'fadeOut',
                    content:'上传成功',
                    duration:'500',
                    isCenter:true,
                });
                if(t==0){
                    that.siblings('input[type="text"]').val(res.mid);
                }else{
                    that.siblings('input[type="text"]').val(res.pathname);
                }
            }
        }
    })
};
$('.inputbutton').click(function(){
        var t=$(this).attr("data-type");
        var name=$(this).siblings("input").val()
        var that=$(this)
        if (name==""){
            return false
        };
        $(this).siblings("input").val("")
        if (t){
        $.ajax({
            url:"/search",
            type:'post',
            data:{t:t,name:name},
            success:function(res){
                var html=""
                if(res){
                    for (var i=0;i<res.length;i++){
                        if (t=="member"){   
                            html+=`<option value="`+res[i].id+`" label="`+res[i].monicker+`-`+res[i].mobile+`"></option>`
                        }else if (t=="company"){
                            html+=`<option value="`+res[i].id+`" label="`+res[i].name+`"></option>`
                        }else if (t=="works"){
                            html+=`<option value="`+res[i].id+`" label="`+res[i].title+`"></option>`
                        }else if (t=="shop"){
                            html+=`<option value="`+res[i].id+`" label="`+res[i].name+`"></option>`
                        }else if (t=="product"){
                            html+=`<option value="`+res[i].id+`" label="`+res[i].title+`"></option>`
                        }
                    }
                }else{
                    html=`<option value="0" label="无结果""></option>`
                }
                that.siblings("datalist").html(html)
            }
        })
        }
    })